package com.ruoyi.goods.controller;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.io.IOException;
import java.util.Optional;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.goods.domain.Category;
import com.ruoyi.goods.domain.Images;
import com.ruoyi.goods.domain.VO.GoodsCateGoryImg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.goods.domain.Goods;
import com.ruoyi.goods.service.IGoodsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品列表Controller
 *
 * @author ruoyi
 * @date 2024-12-20
 */
@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController {
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询商品列表列表
     */
    @RequiresPermissions("goods:goods:list")
    @GetMapping("/list")
    public TableDataInfo list(Goods goods) {
        startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
        return getDataTable(list);
    }
    @GetMapping("/classifyList")
    public TableDataInfo classifyList() {
        List<Category> list = goodsService.classifyList();
        return getDataTable(list);
    }

    /**
     * 导出商品列表列表
     */
    @RequiresPermissions("goods:goods:export")
    @Log(title = "商品列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Goods goods) {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.exportExcel(response, list, "商品列表数据");
    }

    /**
     * 获取商品列表详细信息
     */
    @RequiresPermissions("goods:goods:query")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId) {
        return success(goodsService.selectGoodsByGoodsId(goodsId));
    }

    /**
     * 新增商品列表
     */
    @RequiresPermissions("goods:goods:add")
    @Log(title = "商品列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsCateGoryImg goods) {
        int insertGoods = goodsService.insertGoods(goods);
        Long goodsId = goods.getGoodsId();

        System.out.println("商品最大ID" + goodsId);
        //添加图片
        goods.getImages().setImagesGoodsId(goodsId);
        int images = goodsService.insertImages(goods.getImages());
        System.out.println(images);
        List<Images> list = goodsService.imgList();
        // 使用流找到最大id的Images对象
        Optional<Images> maxIdImage = list.stream()
                .max(Comparator.comparing(Images::getImagesId));
         // 如果需要获取最大id的值
        if (maxIdImage.isPresent()) {
            int maxId = maxIdImage.get().getImagesId();
            System.out.println("图片最大id"+maxId);
            goods.setGoodsImgId((long) maxId);
            // 现在maxId变量包含了最大的id值
        } else {
            System.out.println("列表为空，没有找到最大id");
        }
        goodsService.updateGoods(goods);
        return toAjax(insertGoods);
    }

    /**
     * 修改商品列表
     */
    @RequiresPermissions("goods:goods:edit")
    @Log(title = "商品列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsCateGoryImg goods) {
        goodsService.updateimg(goods.getImages());
        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 删除商品列表
     */
    @RequiresPermissions("goods:goods:remove")
    @Log(title = "商品列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds) {
        return toAjax(goodsService.deleteGoodsByGoodsIds(goodsIds));
    }
}
